package com.datas.service.baseentity;

import com.datas.db.hibernatebeans.UmsRegionProvince;
import com.datas.utils.hbn.HbnTxRuner;
import org.hibernate.Session;
import org.springframework.stereotype.Service;

/**
 * Created by jige0727 on 2017/5/28.
 */
@Service
public class RegionProvinceService {
    public Integer create(UmsRegionProvince dataIn) {
        Integer res = (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                return create(dataIn, getCurrentSession());
            }
        }.runTx();
        return res;
    }

    private Integer create(UmsRegionProvince dataIn, Session session) {
        session.save(dataIn);
        return 1;
    }

    public Integer modify(UmsRegionProvince dataIn) {
        Integer res = (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                return modify(dataIn, getCurrentSession());
            }
        }.runTx();
        return res;
    }

    private Integer modify(UmsRegionProvince dataIn, Session session) {
        session.update(dataIn);
        return 1;
    }

    /**
     * 返回 1成功 0失败，有子节点
     *
     * @param id
     * @return
     */
    public Integer delete(Integer id) {
        Integer res = (Integer) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                UmsRegionProvince data = getCurrentSession().get(UmsRegionProvince.class, id);
                //FIXME 判断关联
                getCurrentSession().delete(data);
                return 1;
            }
        }.runTx();
        return res;
    }

    public UmsRegionProvince info(Integer id) {
        UmsRegionProvince data = (UmsRegionProvince) new HbnTxRuner() {
            @Override
            protected Object dataLogic() throws Exception {
                return getCurrentSession().get(UmsRegionProvince.class, id);
            }
        }.run();
        return data;
    }
}
